class RankScene extends eui.Component  {
    private exml_list: eui.List;
    private friendRank: eui.Label;
    private worldRank: eui.Label;
    private shadowBlock: eui.Rect;
    private deepBlock: eui.Rect;
    private status: number = 1;
    private prevBtn: any;
    private nextBtn: any;
    private backHomeBtn: any;
    private rankList: any;
    private bitmap: any;
    private currentPage = 1;
    private totalPage = 0;
    private collection;
    private allResult;
    private isFirst = true;
    private worldRankText;
    private friendRankText;

    
    public constructor() {
        super();
        this.skinName = 'gameRank';
    }

    protected childrenCreated() {
        this.init()
    }

    private async init() {

        this.status = 1;

        this.allResult = [];

        this.currentPage = 1;

        let platform: any = window.platform;

        this.getWorldRank();

        this.bitmap = platform.openDataContext.createDisplayObject(null, this.stage.stageWidth, this.stage.stageHeight);
        this.showFirendPage();

        //主域向子域发送自定义消息
        platform.openDataContext.postMessage({
            command: "fetchData"
        });

        this.friendRank.addEventListener(egret.TouchEvent.TOUCH_TAP,() => {
            if(this.status === 1) return
            this.friendRankText.textColor = 0xFFFFFF;
            this.worldRankText.textColor = 0x000000;
            this.shadowBlock.x += 50;
            this.deepBlock.x -= 238;
            this.status = 1;

            this.showFirendPage()

            //主域向子域发送自定义消息
            platform.openDataContext.postMessage({
                command: "open"
            });
        },this);

        this.worldRank.addEventListener(egret.TouchEvent.TOUCH_TAP,() => {
            if(this.status === 2) return
            this.worldRankText.textColor = 0xFFFFFF;
            this.friendRankText.textColor = 0x000000;
            this.shadowBlock.x -= 50;
            this.deepBlock.x += 238;
            this.status = 2;

            this.showWorldPage();
            platform.openDataContext.postMessage({
                command: "close"
            });
        },this);

        this.backHomeBtn.addEventListener(egret.TouchEvent.TOUCH_TAP,() => {
            SceneMange.getInstance().changeScene('beginScene');
        },this);

    }
    
    private showFirendPage() {
        this.removeChild(this.rankList);
        this.removeChild(this.prevBtn);
        this.removeChild(this.nextBtn);
        this.addChild(this.bitmap);
        this.nextBtn.removeEventListener(egret.TouchEvent.TOUCH_TAP,this.nextPage,this);
        this.prevBtn.removeEventListener(egret.TouchEvent.TOUCH_TAP,this.prevPage,this);
    }

    private showWorldPage() {
        this.addChild(this.rankList);
        this.addChild(this.prevBtn);
        this.addChild(this.nextBtn);
        this.removeChild(this.bitmap);
        this.nextBtn.addEventListener(egret.TouchEvent.TOUCH_TAP,this.nextPage,this);
        this.prevBtn.addEventListener(egret.TouchEvent.TOUCH_TAP,this.prevPage,this);
    }

    private nextPage() {
        if(this.currentPage === this.totalPage) return;
        this.currentPage ++;
        if(this.currentPage === this.totalPage) {
            this.nextBtn.texture = RES.getRes(`nextBtn2_png`)
        }
        this.prevBtn.texture = RES.getRes(`preBtn_png`)
        let currentShow = this.allResult.slice(0 + (9*(this.currentPage-1)), 9* this.currentPage)
        this.collection.replaceAll(currentShow)
        this.exml_list.dataProvider = this.collection;
    }

    private prevPage() {
        if(this.currentPage === 1) return;
        this.currentPage --;
        if(this.currentPage === 1) {
            this.prevBtn.texture = RES.getRes(`preBtn2_png`)
        }
        this.nextBtn.texture = RES.getRes(`nextBtn_png`)
        let currentShow = this.allResult.slice(0 + (9*(this.currentPage-1)), 9* this.currentPage)
        this.collection.replaceAll(currentShow)
        this.exml_list.dataProvider = this.collection;
    }

    public async reset() {
        if(this.isFirst) {
            this.isFirst = false;
            return;
        }
        this.currentPage = 1;
        let platform: any = window.platform;
        if(this.status === 2){
            this.friendRankText.textColor = 0xFFFFFF;
            this.worldRankText.textColor = 0x000000;
            this.shadowBlock.x += 50;
            this.deepBlock.x -= 238;
            this.status = 1;
            this.showFirendPage();
        }
        platform.openDataContext.postMessage({
            command: "fetchData"
        });
        this.getWorldRank();
    }

    private async getWorldRank() {
        try {
            let result: any = await platform.request({
                url: platform.globalPath + '/score/worldRank',
                method: 'GET',
                data: {pageIndex: 1, pageSize: 200}
            })
            if(!result.data.success) return;
            this.allResult = result.data.data;

            this.totalPage = Math.ceil(this.allResult.length / 9) !== 0 ? Math.ceil(this.allResult.length / 9) : 1;

            if(this.currentPage === this.totalPage) {
                this.nextBtn.texture = RES.getRes(`nextBtn2_png`)
            }

            if(this.allResult[0]) {
                this.allResult[0]['top'] = '1'
                this.allResult[0]['img'] = 'rank1_png'
                this.allResult[0]['rank'] = ''
            }
            if(this.allResult[1]) {
                this.allResult[1]['top'] = '2'
                this.allResult[1]['img'] = 'rank2_png'
                this.allResult[1]['rank'] = ''
            }
            if(this.allResult[2]) {
                this.allResult[2]['top'] = '3'
                this.allResult[2]['img'] = 'rank3_png'
                this.allResult[2]['rank'] = ''
            }
            this.allResult.forEach((item) => {
                item.nickName = item.nickName.substring(0,8)
            })
             //获取需要展示的数据
            let currentShow = this.allResult.slice(0 + (9*(this.currentPage-1)), 9* this.currentPage)
            this.collection = this.collection || new eui.ArrayCollection(currentShow);
            this.collection.replaceAll(currentShow)
            this.exml_list.dataProvider = this.collection;
        } catch (error) {
            
        }
    }
}